package per.mapt.webservice.vo;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by mapei on 2017/3/18.
 */
public class IntegerUserMapAdapter extends XmlAdapter<IntegerUserMap,Map<Integer,User>> {
    @Override
    public Map<Integer, User> unmarshal(IntegerUserMap v) throws Exception {
        Map<Integer,User> result = new LinkedHashMap<>(v.getEntries().size());
        for(IntegerUserMap.IntegerUserEntry e:v.getEntries()){
            result.put(e.getId(),e.getUser());
        }
        return result;
    }

    @Override
    public IntegerUserMap marshal(Map<Integer, User> v) throws Exception {
        IntegerUserMap userMap = new IntegerUserMap();
        if(v!=null){
            for (Map.Entry<Integer, User> e:v.entrySet()) {
                IntegerUserMap.IntegerUserEntry entry =new IntegerUserMap.IntegerUserEntry();
                entry.setId(e.getKey());
                entry.setUser(e.getValue());
                userMap.getEntries().add(entry);
            }
        }
        return userMap;
    }
}
